package com.wxq.demo.controller;

import com.wxq.demo.pojo.Book;
import com.wxq.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/findAll")
    public void findAll() {
        //第一个参数表示页数，从0开始；第二个参数表示每页显示条数
        PageRequest pageRequest = PageRequest.of(2, 3);
        Page<Book> page = bookService.getBookByPage(pageRequest);
        System.out.println("总页数：" + page.getTotalPages());
        System.out.println("总记录数：" + page.getTotalElements());
        System.out.println("查询结果：" + page.getContent());
        System.out.println("当前页数：" + page.getNumber() + 1);
        System.out.println("当前页总记录数：" + page.getNumberOfElements());
        System.out.println("每页记录数：" + page.getSize());
    }

    @GetMapping("/search")
    public void search() {
        System.out.println(bookService.getBookByidAndAuthor("鲁迅", 7));
        System.out.println(bookService.getBooksByAuthorStartingWith("吴"));
        System.out.println(bookService.getBooksByidAndName("西", 8));
        System.out.println(bookService.getBooksByPriceGreaterThan(30F));
        System.out.println(bookService.getMaxidBook());
    }

    @GetMapping("/save")
    public void save() {
        Book book = new Book();
        book.setAuthor("鲁迅");
        book.setName("呐喊");
        book.setPrice(23F);
        bookService.addBook(book);
    }

}
